-- UIBookshelf
-- Created by chendh Oct/19/2015
-- 废弃的书架

UIBookshelf = class("UIBookshelf", function()
    return cc.Layer:create();
end);

function UIBookshelf.create(gridData, title, dialog, iconPath, iconOffsetY, canClickFunc, clickFunc, btnTextList)
    return UIBookshelf.new(gridData, title, dialog, iconPath, iconOffsetY, canClickFunc, clickFunc, btnTextList);
end

function UIBookshelf:ctor(gridData, title, dialog, iconPath, iconOffsetY, canClickFunc, clickFunc, btnTextList)
    -- 初始化
    self:setName("UIBookshelf");
    local node = cc.CSLoader:createNode("layout/dungeon/special/Bookshelf.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    assert(type(gridData) == "table", "必须传入格子数据");

    self.gridData  = gridData;

    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    self.title          = title or buildingInfo["title"];                               -- 标题
    self.iconPath       = iconPath or getLevelItemIconPath(buildingInfo["icon"]);       -- 图标路径
    self.dialog         = dialog or BuildingBonusM.getBuildingDialog(self.gridData);    -- 对话内容
    self.iconOffsetY    = iconOffsetY or 0;                                             -- 图标纵向偏移量
    self.canClickFunc   = canClickFunc;                                                 -- 是否可以点击的判断函数
    self.clickFunc      = clickFunc;                                                    -- 点击回调函数
    self.btnTextList    = btnTextList;                                                  -- 按钮名称列表

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 初始化按钮列表
    self:initBtnGrids();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIBookshelf:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIBookshelf:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBookshelf 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIBookshelf:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBookshelf");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end

-- 初始化按钮
function UIBookshelf:initBtnGrids()
    local btnNum = #self.btnTextList;
    local panelNode = findChildByName(self.node, "CT/panel");

    panelNode.rows = {};
    panelNode:removeAllChildren();

    local itemNode  = cc.CSLoader:createNode("layout/common/Button.csb");
    local sortType  = "Horizontal";
    local maxRowNum = 3;
    local maxColNum = 2;
    local wGap      = 20;
    local hGap      = 10;
    local iconWidth = 210;
    local iconHeight = 115;
    local topHGap   = 10;
    local leftWGap  = 20;
    -------------------------------------------------------------------------------
    -- 初始化普通容器内的单元格
    -- @param windowNode    界面节点
    -- @param panelNode     容器节点
    -- @param itemNode      单元格的节点
    -- @param totalNum      单元格总数
    -- @param sortType      排列方式
    -- @param maxRowNum     最大行数
    -- @param maxColNum     最大列数
    -- @param wGap          单元格间距(横向)
    -- @param hGap          单元格间距(纵向)
    -- @param iconWidth     单元格宽度
    -- @param iconHeight    单元格高度
    -- @param topHGap       上下留白
    -- @param leftWGap      左右留白
    -- @param touchEvent    点击事件注册函数
    initPanelGrids(self, panelNode, itemNode, btnNum, "Horizontal", maxRowNum, maxColNum, wGap, hGap, iconWidth, iconHeight, topHGap, leftWGap, self.registerBtnTouchEvent)
end

-- 注册按钮点击事件
function UIBookshelf:registerBtnTouchEvent(node)
    local btnNode = findChildByName(node, "button");
    local function onClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            local index = node.index;

            self.clickFunc(self.gridData, index);
        end
    end

    AddTouchEventListener(btnNode, onClick);
end

-- 重绘
function UIBookshelf:redraw()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleNode  = findChildByName(self.node, "CT/title_node");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local panelNode = findChildByName(self.node, "CT/panel");

    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    iconImg:loadTexture(self.iconPath);
    iconImg:setPositionY(iconImg:getPositionY() + self.iconOffsetY);
    iconImg:setScale(1.3);
    iconImg:setVisible(false);

    titleLabel:setString(self.title);
    TextStyleM.setArtWordTitleStyle(titleLabel);

    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
    applyString(descLabel, self.dialog);
    local descLabel  = findChildByName(self.node, "CT/desc");

    local totalCount = #self.btnTextList;
    for i = 1, totalCount do
        local grid = panelNode:getChildByName("item" .. i);
        local buttonNode = grid:getChildByName("button");
        local index = grid.index;

        TextStyleM.setTextStyle(buttonNode, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        buttonNode:setTitleText(self.btnTextList[index]);

        buttonNode:setTouchEnabled(true);
        buttonNode:setBright(true);

        if self.canClickFunc then
            if not self.canClickFunc(self.gridData, index) then
                buttonNode:setTouchEnabled(false);
                buttonNode:setBright(false);
            end
        end
    end

    -- 自动布局
    local bg = findChildByName(self.node, "CT/bg1");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL, { titleNode, descLabel, panelNode },
        {}, -50);
end
